package com.emm.yixun.customer.service.impl.app.manage;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.emm.yixun.common.model.*;
import com.emm.yixun.common.response.ResponseList;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.emm.yixun.basic.mapper.SalesGroupMapper;
import com.emm.yixun.basic.service.AppPushService;
import com.emm.yixun.basic.service.CustomerService;
import com.emm.yixun.basic.service.DictionaryService;
import com.emm.yixun.basic.service.DistributionService;
import com.emm.yixun.basic.service.FollowService;
import com.emm.yixun.basic.service.MessageReceiverService;
import com.emm.yixun.basic.service.NoticeService;
import com.emm.yixun.basic.service.PayDepositService;
import com.emm.yixun.basic.service.ProjectService;
import com.emm.yixun.basic.service.SignedService;
import com.emm.yixun.basic.service.SubscribeService;
import com.emm.yixun.basic.service.UserService;
import com.emm.yixun.common.app.DistributionConsultantReqDto;
import com.emm.yixun.common.app.DistributionConsultantResDto;
import com.emm.yixun.common.app.base.CommonResDTO;
import com.emm.yixun.common.app.servletentity.CustomerListReq;
import com.emm.yixun.common.enums.ResultEnum;
import com.emm.yixun.common.model.dto.CustomerDto;
import com.emm.yixun.common.model.dto.DictionaryDto;
import com.emm.yixun.common.model.dto.DistributionDto;
import com.emm.yixun.common.model.dto.FollowDto;
import com.emm.yixun.common.model.dto.MessageReceiverDto;
import com.emm.yixun.common.model.dto.NoticeDto;
import com.emm.yixun.common.model.dto.ProjectDto;
import com.emm.yixun.common.model.enums.DelFlagEnum;
import com.emm.yixun.common.model.enums.FollowModeEnum;
import com.emm.yixun.common.model.enums.FollowTypeEnum;
import com.emm.yixun.common.model.enums.IsDistributionEnum;
import com.emm.yixun.common.model.enums.IsSendEnum;
import com.emm.yixun.common.model.enums.IsValidEnum;
import com.emm.yixun.common.model.enums.ProcessStatusEnum;
import com.emm.yixun.common.model.enums.PublicSignsEnum;
import com.emm.yixun.common.utils.MsgUtil;

@Service("distributionConsultantManage")
public class DistributionConsultantManage {

	private static Logger logger = LoggerFactory.getLogger(DistributionConsultantManage.class);

	@Autowired
	private DistributionService distributionService;
	@Autowired
	private CustomerService customerService;
	@Autowired
	private UserService userService;
	@Autowired
	private PayDepositService payDepositService;
	@Autowired
	private SubscribeService subscribeService;
	@Autowired
	private SignedService signedService;
	@Autowired
	private FollowService followService;
	@Autowired
	private DictionaryService dictionaryService;
	@Autowired
	private CusAppNoticeManage cusAppNoticeManage;
	@Autowired
	private MessageReceiverService messageReceiverService;
	@Autowired
	private AppPushService appPushService;
	@Autowired
	private NoticeService noticeService;
	@Autowired
	private SalesGroupMapper salesGroupMapper;
	@Autowired
	private ProjectService projectService;

	/**
	 * 分配置业顾问列表
	 * 
	 * @param reqDto
	 * @return
	 */
	public DistributionConsultantResDto distributionConsultant(DistributionConsultantReqDto reqDto) {
		DistributionConsultantResDto resDto = new DistributionConsultantResDto();
		try {
			// 分配职业顾问处理流程
			// 1、添加分配记录
			// 1.1、获取当前有效的分配记录
			// 1.1.1、无有效记录,插入分配记录并修改客户信息
			// 1.1.2、有有效记录,修改此记录为无效并插入新分配记录
			// 2、修改客户信息下部分字段

			List<CustomerListReq> customerList = reqDto.getCustomerList();
			User loginUser = userService.selectById(Long.valueOf(reqDto.getUserId()));

			for (CustomerListReq req : customerList) {
				String customerId = req.getCustomerId();
				// 修改客户信息
				Customer oldCustomer = customerService.selectById(Long.parseLong(customerId));
				if (null == oldCustomer) {
					resDto.setErrorCode("00000");
					resDto.setErrorMsg("分配经纪人失败,客户不存在!");
					resDto.setResult(ResultEnum.FAILURE.getNo());
					return resDto;
				}

				User user_jjr = userService.selectById(Long.valueOf(reqDto.getConsultantUserId()));// 经纪人
				if(null == user_jjr){
					resDto.setErrorCode("00000");
					resDto.setErrorMsg("当前经纪人无效,请重新选择!");
					resDto.setResult(ResultEnum.FAILURE.getNo());
					return resDto;
				}

				// 同一个项目之间分配(原项目中的分配记录变更为无效)
				DistributionDto distributionDto = new DistributionDto();
				distributionDto.setCustomerId(Long.parseLong(customerId));
				distributionDto.setProjectCode(oldCustomer.getProjectCode());
				distributionDto.setIsValid(IsValidEnum.VALID.getNo());
				distributionDto.setMerchantId(reqDto.getMerchantId());
				Distribution distribution = distributionService.selectByDto(distributionDto);
				if (distribution != null) {
					// 将记录修改为无效
					distributionDto = new DistributionDto();
					distributionDto.setId(distribution.getId());
					distributionDto.setIsValid(IsValidEnum.UNVALID.getNo());
					distributionService.update(distributionDto);
				}

				if(reqDto.getProjectId().compareTo(oldCustomer.getProjectId()) == 0){

					//添加分配记录
					createNewDistribution(oldCustomer, reqDto, distribution, loginUser);

					// is_distribution 是否分配
					// public_signs 公私标志
					// distribution_num 分配次数
					// process_status 处理状态
					long distributionNum = oldCustomer.getDistributionNum() == null ? 0 : oldCustomer.getDistributionNum();
					CustomerDto customerDto = new CustomerDto();
					customerDto.setId(oldCustomer.getId());
					customerDto.setIsDistribution(IsDistributionEnum.DISTRIBUTION.getNo());
					customerDto.setPublicSigns(PublicSignsEnum.PRIVATE.getNo());
					customerDto.setDistributionNum(distributionNum + 1);
					customerDto.setProcessStatus(ProcessStatusEnum.YES.getNo());
					// 因为系统发的跟进内容不允许修改客户最后跟进时间，故注释下行代码
					// customerDto.setLastFollowTime(new Date());//分配成功后，初始化最后跟进时间为当前时间
					Long salesGroupId = salesGroupMapper.selectGroupId(reqDto.getMerchantId(), reqDto.getProjectCode(),
							user_jjr.getId());
					if (salesGroupId != null) {
						customerDto.setSalesGroupId(salesGroupId);
					}
					customerDto.setUserCode(user_jjr.getUserCode());
					//20190111 jzliu “是否已放弃状态”原来被注释掉了，导致客户池分配后仍在客户池，但是不知道出于什么原因
					//customerDto.setGiveUpStatus("0"); //分配为有效客户 LSH
					customerDto.setGiveUpStatus("0");
					customerService.update(customerDto);

					// 分配成功，插入系统默认跟进记录
					FollowDto followDto = new FollowDto();
					followDto.setCustomerId(Long.valueOf(customerId));
					followDto.setProjectId(reqDto.getProjectId());
					followDto.setProjectCode(reqDto.getProjectCode());
					followDto.setFollowMode(FollowModeEnum.SYSTEMGENERATION.getNo());
					followDto.setFollowTime(new Date());
					followDto.setFollowType(FollowTypeEnum.TEXT.getNo());
					String sgjjr = null;
					if (distribution != null) {
						if (distribution.getAssignPerson() != null && !"".equals(distribution.getAssignPerson())) {
							User user_sgjjr = userService.selectById(Long.valueOf(distribution.getAssignPerson()));// 上一个经纪人
							if (user_sgjjr != null) {
								sgjjr = user_sgjjr.getUserName();
							}
						}
					}
					if (sgjjr == null) {
						followDto.setFollowContent(loginUser.getUserName() + "将" + oldCustomer.getCustomerName() + "分配给"
								+ user_jjr.getUserName() + "\n归属人变为" + user_jjr.getUserName());
					} else {
						followDto.setFollowContent(loginUser.getUserName() + "将" + oldCustomer.getCustomerName() + "分配给"
								+ user_jjr.getUserName() + "\n归属人" + sgjjr + "变为" + user_jjr.getUserName());
					}
					followDto.setMerchantId(reqDto.getMerchantId());
					followDto.setUserCode(loginUser.getUserCode());
					followDto.setAccessCode(reqDto.getProjectCode());
					followDto.setRemark("添加系统生成跟进记录");
					followDto.setCreateUserId(Long.parseLong(reqDto.getUserId()));
					followDto.setCreateTime(new Date());
					followDto.setDelFlag(DelFlagEnum.UNDEL.getNo());
					followService.create(followDto);

					// 分配客户后创建消息
					String customerName = oldCustomer.getCustomerName();
					String userName = loginUser.getUserName();
					String newUserName = user_jjr.getUserName();
					User userEntity = userService.selectById(Long.valueOf(oldCustomer.getCreateUserId()));//创建人
					CommonResDTO commonResDto = createDistNotice(distributionDto, customerName, userName, newUserName, userEntity, oldCustomer);
					if ("1".equals(commonResDto.getResult())) {
						// 推送消息
						//pushMsg(customer.getProjectCode());
					}
				}else{
					//非配到其他项目
					distributionToOtherProject(oldCustomer, reqDto, loginUser, user_jjr, distribution);
				}
			}
			resDto.setResult(ResultEnum.SUCCESS.getNo());
		} catch (Exception e) {
			e.printStackTrace();
			resDto.setErrorCode("00000");
			resDto.setErrorMsg("分配经纪人失败,未知异常!");
			resDto.setResult(ResultEnum.FAILURE.getNo());
			return resDto;
		}
		return resDto;
	}

	public DistributionDto createNewDistribution(Customer customer, DistributionConsultantReqDto reqDto, Distribution distribution, User loginUser){
		// 插入新分配记录
		DistributionDto distributionDto = new DistributionDto();
		distributionDto.setCustomerId(customer.getId());
		distributionDto.setProjectId(reqDto.getProjectId());
		distributionDto.setProjectCode(reqDto.getProjectCode());
		distributionDto.setLastAssign(distribution == null ? "" : distribution.getAssignPerson());
		distributionDto.setAssignPerson(reqDto.getConsultantUserId());
		distributionDto.setIsValid(IsValidEnum.VALID.getNo());
		distributionDto.setDistributionTime(Calendar.getInstance().getTime());
		distributionDto.setMerchantId(reqDto.getMerchantId());
		distributionDto.setUserCode(loginUser.getUserCode());
		distributionDto.setAccessCode(reqDto.getProjectCode());
		distributionDto.setRemark("添加分配记录");
		distributionDto.setCreateUserId(Long.parseLong(reqDto.getUserId()));
		distributionDto.setCreateTime(new Date());
		distributionDto.setDelFlag(DelFlagEnum.UNDEL.getNo());
		//20190111 jzliu 分配后直接变成已处理
		//distributionDto.setProcessStatus(ProcessStatusEnum.NO.getNo());
		distributionDto.setProcessStatus(ProcessStatusEnum.YES.getNo());
		return distributionService.create(distributionDto);
	}

	public DistributionConsultantResDto distributionToOtherProject(Customer oldCustomer, DistributionConsultantReqDto reqDto, User loginUser, User agentUser, Distribution distribution){
		//判断客户在目标项目是否存在
		DistributionConsultantResDto resDto = new DistributionConsultantResDto();
		CustomerDto customerDto = new CustomerDto();
		customerDto.setProjectId(reqDto.getProjectId());
		customerDto.setCustomerPhone(oldCustomer.getCustomerPhone()); //手机号码重复
		Customer newCustomer = customerService.selectByDto(customerDto);
		if(null == newCustomer){
			//新增客户
			customerDto = new CustomerDto();
			BeanUtils.copyProperties(oldCustomer, customerDto);
			customerDto.setId(null);
			customerDto.setIsDistribution(IsDistributionEnum.DISTRIBUTION.getNo());
			customerDto.setPublicSigns(PublicSignsEnum.PRIVATE.getNo());
			customerDto.setDistributionNum(1L);
			customerDto.setProcessStatus(ProcessStatusEnum.YES.getNo());
			// 因为系统发的跟进内容不允许修改客户最后跟进时间，故注释下行代码
			// customerDto.setLastFollowTime(new Date());//分配成功后，初始化最后跟进时间为当前时间
			Long salesGroupId = salesGroupMapper.selectGroupId(reqDto.getMerchantId(), reqDto.getProjectCode(),
					agentUser.getId());
			if (salesGroupId != null) {
				customerDto.setSalesGroupId(salesGroupId);
			}
			customerDto.setProjectId(reqDto.getProjectId());
			customerDto.setProjectCode(reqDto.getProjectCode());
			customerDto.setAccessCode(reqDto.getProjectCode());
			customerDto.setUserCode(agentUser.getUserCode());
			//20190111 jzliu “是否已放弃状态”原来被注释掉了，导致客户池分配后仍在客户池，但是不知道出于什么原因
			//customerDto.setGiveUpStatus("0"); //分配为有效客户 LSH
			customerDto.setGiveUpStatus("0");
			newCustomer = customerService.create(customerDto);

			//目标项目添加分配记录
			DistributionDto newDistributionDto = createNewDistribution(newCustomer, reqDto, distribution, loginUser);

			//将原客户跟进写入到新客户中
			FollowDto followDto = new FollowDto();
			followDto.setCustomerId(oldCustomer.getId());
			List<Follow> followList = followService.selectListByDto(followDto);
			if(null != followList && followList.size() > 0){
				for(Follow follow: followList){
					followDto = new FollowDto();
					BeanUtils.copyProperties(follow, followDto);
					followDto.setId(null);
					followDto.setCustomerId(newCustomer.getId());
					followDto.setProjectId(reqDto.getProjectId());
					followDto.setProjectCode(reqDto.getProjectCode());
					followDto.setUserCode(agentUser.getUserCode());
					followDto.setAccessCode(reqDto.getProjectCode());
					followDto.setCreateUserId(agentUser.getId());
					followService.create(followDto);
				}
			}

			// 分配成功，插入系统默认跟进记录
			followDto = new FollowDto();
			followDto.setCustomerId(newCustomer.getId());
			followDto.setProjectId(reqDto.getProjectId());
			followDto.setProjectCode(reqDto.getProjectCode());
			followDto.setFollowMode(FollowModeEnum.SYSTEMGENERATION.getNo());
			followDto.setFollowTime(new Date());
			followDto.setFollowType(FollowTypeEnum.TEXT.getNo());
			String sgjjr = null;
			if (distribution != null) {
				if (distribution.getAssignPerson() != null && !"".equals(distribution.getAssignPerson())) {
					User user_sgjjr = userService.selectById(Long.valueOf(distribution.getAssignPerson()));// 上一个经纪人
					if (user_sgjjr != null) {
						sgjjr = user_sgjjr.getUserName();
					}
				}
			}
			if (sgjjr == null) {
				followDto.setFollowContent(loginUser.getUserName() + "将" + oldCustomer.getCustomerName() + "分配给"
						+ agentUser.getUserName() + "\n归属人变为" + agentUser.getUserName());
			} else {
				followDto.setFollowContent(loginUser.getUserName() + "将" + oldCustomer.getCustomerName() + "分配给"
						+ agentUser.getUserName() + "\n归属人" + sgjjr + "变为" + agentUser.getUserName());
			}
			followDto.setMerchantId(reqDto.getMerchantId());
			followDto.setUserCode(agentUser.getUserCode());
			followDto.setAccessCode(reqDto.getProjectCode());
			followDto.setRemark("添加系统生成跟进记录");
			followDto.setCreateUserId(agentUser.getId());
			followDto.setCreateTime(new Date());
			followDto.setDelFlag(DelFlagEnum.UNDEL.getNo());
			followService.create(followDto);

			// 删除原客户记录
			customerDto = new CustomerDto();
			customerDto.setId(oldCustomer.getId());
			customerDto.setDelFlag(DelFlagEnum.DEL.getNo());
			customerService.update(customerDto);

			// 分配客户后创建消息
			String customerName = oldCustomer.getCustomerName();
			String userName = loginUser.getUserName();
			String newUserName = agentUser.getUserName();
			User userEntity = userService.selectById(Long.valueOf(oldCustomer.getCreateUserId()));//创建人
			DistributionDto distributionDto = new DistributionDto();
			if(null != distribution){
				BeanUtils.copyProperties(distribution, distributionDto);
			}else{
				distributionDto = newDistributionDto;
			}
			CommonResDTO commonResDto = createDistNotice(distributionDto, customerName, userName, newUserName, userEntity, oldCustomer);
			if ("1".equals(commonResDto.getResult())) {
				// 推送消息
				//pushMsg(customer.getProjectCode());
			}
		}else{
			resDto.setErrorCode("00000");
			resDto.setErrorMsg("目标项目中客户存在,不能分配!");
			resDto.setResult(ResultEnum.FAILURE.getNo());
			return resDto;
		}
		resDto.setResult(ResultEnum.SUCCESS.getNo());
		return resDto;
	}

	public CommonResDTO createDistNotice(DistributionDto distributionDto, String customerName, String userName, String newUserName, User userEntity, Customer customer) {
		CommonResDTO resDto = new CommonResDTO();
		resDto.setResult(ResultEnum.FAILURE.getNo());
		try {
			String action = "待处理客户实时提醒";
			//String pushType = "DCLKHSSTX";
			// 根据分配记录,添加消息
			String msgTemp = "${userName}将${customer}分配给你,快去处理吧！";
			String sendMsg = "";
			DictionaryDto dicDto = new DictionaryDto();
			dicDto.setDictionaryValue("NT06");
			Dictionary dic = dictionaryService.selectByDto(dicDto);
			if (dic != null) {
				msgTemp = dic.getDictionaryName();
			}

			sendMsg = msgTemp.replace("${userName}", userName).replace("${customer}", customerName);

			// 创建提醒消息及消息发送人信息
			Map<String, String> noticeParamMap = new HashMap<String, String>();
			noticeParamMap.put("noticeDicId", "165");
			noticeParamMap.put("noticeTempType", "NT06");
			noticeParamMap.put("sendMsg", sendMsg);
			noticeParamMap.put("title", "客户分配: 被分配,得到客户");
			noticeParamMap.put("merchantId", String.valueOf(distributionDto.getMerchantId()));
			noticeParamMap.put("projectCode", distributionDto.getProjectCode());
			User user = userService.selectById(Long.valueOf(distributionDto.getAssignPerson()));
			noticeParamMap.put("userCode", user.getUserCode());
			noticeParamMap.put("accessCode", distributionDto.getAccessCode());
			noticeParamMap.put("createUserId", String.valueOf(distributionDto.getAssignPerson()));
			noticeParamMap.put("customerId", customer.getId()+"");
			/*
			 * (ym) 提醒内容增加超链接 start
			 */
			Map<String, String> replaceValuesMap = new HashMap<String, String>();
			replaceValuesMap.put(MsgUtil.ROOM_ID, "");
			replaceValuesMap.put(MsgUtil.ROOM_NAME, "");
			replaceValuesMap.put(MsgUtil.CUSTOMER_ID, String.valueOf(distributionDto.getCustomerId()));
			replaceValuesMap.put(MsgUtil.CUSTOMER_NAME, customerName);
			replaceValuesMap.put(MsgUtil.PRICE_SCHEME_ID, "");
			replaceValuesMap.put(MsgUtil.PRICE_SCHEME_NAME, "");
			noticeParamMap.put("replaceValues", MsgUtil.getReplaceValues(replaceValuesMap));
			/*
			 * (ym) 提醒内容增加超链接 end
			 */

			List<User> userList = new ArrayList<User>();
			userList.add(userService.selectById(Long.parseLong(distributionDto.getAssignPerson())));
			CommonResDTO commonResDto = new CommonResDTO();
			commonResDto = cusAppNoticeManage.createNotice(noticeParamMap, userList, action);
			if ("1".equals(commonResDto.getResult())) {
				logger.info("客户分配: 被分配,得到客户消息创建成功");
				resDto.setResult("1");
			}
			// 判断是否存在上一个经纪人,存在进行通知
			if (StringUtils.isNotEmpty(distributionDto.getLastAssign())) {
				msgTemp = "客户${customer}已经被${userName}分配给${newUserName}。";
				sendMsg = "";
				dicDto = new DictionaryDto();
				dicDto.setDictionaryValue("NT04");
				dic = dictionaryService.selectByDto(dicDto);
				if (dic != null) {
					msgTemp = dic.getDictionaryName();
				}
				sendMsg = msgTemp.replace("${customer}", customerName).replace("${userName}", userName).replace("${newUserName}", newUserName);

				Map<String, String> noticeParamMap2 = new HashMap<String, String>();
				noticeParamMap2.put("noticeDicId", "163");
				noticeParamMap2.put("noticeTempType", "NT04");
				noticeParamMap2.put("sendMsg", sendMsg);
				noticeParamMap2.put("title", "客户分配: 被分配,流失客户");
				noticeParamMap2.put("merchantId", String.valueOf(distributionDto.getMerchantId()));
				noticeParamMap2.put("projectCode", distributionDto.getProjectCode());
				User user2 = userService.selectById(Long.valueOf(distributionDto.getLastAssign()));
				noticeParamMap2.put("userCode", user2.getUserCode());
				noticeParamMap2.put("accessCode", distributionDto.getAccessCode());
				noticeParamMap2.put("createUserId", String.valueOf(distributionDto.getLastAssign()));
				noticeParamMap2.put("customerId", customer.getId()+"");

				noticeParamMap2.put("replaceValues", MsgUtil.getReplaceValues(replaceValuesMap));
				List<User> userList2 = new ArrayList<User>();
				userList2.add(userService.selectById(Long.parseLong(distributionDto.getLastAssign())));

				commonResDto = new CommonResDTO();
				commonResDto = cusAppNoticeManage.createNotice(noticeParamMap2, userList2, action);
				resDto.setResult(commonResDto.getResult());
			}else{
				msgTemp = "客户${customer}已经被${userName}分配给${newUserName}。";
				sendMsg = "";
				dicDto = new DictionaryDto();
				dicDto.setDictionaryValue("NT04");
				dic = dictionaryService.selectByDto(dicDto);
				if (dic != null) {
					msgTemp = dic.getDictionaryName();
				}
				sendMsg = msgTemp.replace("${customer}", customerName).replace("${userName}", userName).replace("${newUserName}", newUserName);

				Map<String, String> noticeParamMap2 = new HashMap<String, String>();
				noticeParamMap2.put("noticeDicId", "163");
				noticeParamMap2.put("noticeTempType", "NT04");
				noticeParamMap2.put("sendMsg", sendMsg);
				noticeParamMap2.put("title", "客户分配: 被分配,流失客户");
				noticeParamMap2.put("merchantId", String.valueOf(customer.getMerchantId()));
				noticeParamMap2.put("projectCode", customer.getProjectCode());
				noticeParamMap2.put("userCode", userEntity.getUserCode());
				noticeParamMap2.put("accessCode", customer.getAccessCode());
				noticeParamMap2.put("createUserId", String.valueOf(userEntity.getId()));
				noticeParamMap2.put("customerId", customer.getId()+"");

				noticeParamMap2.put("replaceValues", MsgUtil.getReplaceValues(replaceValuesMap));
				List<User> userList2 = new ArrayList<User>();
				userList2.add(userService.selectById(userEntity.getId()));

				commonResDto = new CommonResDTO();
				commonResDto = cusAppNoticeManage.createNotice(noticeParamMap2, userList2, action);
				resDto.setResult(commonResDto.getResult());
			}
		} catch (Exception e) {
			e.printStackTrace();
			resDto.setErrorCode("00000");
			resDto.setErrorMsg("分配经纪人消息通知失败," + e);
			resDto.setResult(ResultEnum.FAILURE.getNo());
			return resDto;
		}
		return resDto;
	}

	public void pushMsg(String projectCode) throws Exception {
		// 1、查询待处理客户相关推送消息
		// NT04: 客户分配: 被分配,流失客户
		// NT05: 客户分配: 被分配,得到客户
		List<MessageReceiverDto> msgRecList = new ArrayList<MessageReceiverDto>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar calendar = Calendar.getInstance();
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH) + 1;
		int day = calendar.get(Calendar.DAY_OF_MONTH);
		// String issueTimeBefore = year + "-" + month + "-" + day + " 00:00:00";
		String issueTimeAfter = year + "-" + month + "-" + day + " 23:59:59";
		// 查询待处理客户,流失客户推送消息
		MessageReceiverDto loseCusMrDto = new MessageReceiverDto();
		loseCusMrDto.setNoticeTempType("NT04");
		loseCusMrDto.setIsSend(IsSendEnum.UNSEND.getNo());
		// loseCusMrDto.setIssueTimeBefore(sdf.parse(issueTimeBefore));
		// 推送所有小于 issueTimeAfter 时间的所有消息
		loseCusMrDto.setIssueTimeAfter(sdf.parse(issueTimeAfter));
		List<MessageReceiverDto> mrLoseList = messageReceiverService.selectPushMsgListByDto(loseCusMrDto);
		if (mrLoseList != null) {
			for (MessageReceiverDto mrLose : mrLoseList) {
				msgRecList.add(mrLose);
			}
		}
		// 查询待处理客户,得到客户推送消息
		MessageReceiverDto getCusMrDto = new MessageReceiverDto();
		getCusMrDto.setNoticeTempType("NT06");
		getCusMrDto.setIsSend(IsSendEnum.UNSEND.getNo());
		// getCusMrDto.setIssueTimeBefore(sdf.parse(issueTimeBefore));
		// 推送所有小于 issueTimeAfter 时间的所有消息
		getCusMrDto.setIssueTimeAfter(sdf.parse(issueTimeAfter));
		List<MessageReceiverDto> mrGetList = messageReceiverService.selectPushMsgListByDto(getCusMrDto);
		if (mrGetList != null) {
			for (MessageReceiverDto mrGet : mrGetList) {
				msgRecList.add(mrGet);
			}
		}

		if (msgRecList != null && msgRecList.size() > 0) {
			String noticeIds = "";
			String msgRecIds = "";
			Set<String> noticeIdSet = new HashSet<String>();
			for (MessageReceiverDto msgRecDto : msgRecList) {
				// 开始推送消息
				Map<String, String> pushMap = new HashMap<String, String>();
				pushMap.put("action", "待处理客户实时提醒");
				pushMap.put("remindCategoryId", String.valueOf(msgRecDto.getDictionaryId()));
				// pushMap.put("pushType", msgRecDto.getNoticeTempType());
				pushMap.put("pushType", "DCLKHSSTX");
				ProjectDto entity = new ProjectDto();
				entity.setProjectCode(projectCode);
				Project project = projectService.selectByDto(entity);
				pushMap.put("projectCode", projectCode);
				pushMap.put("projectName", project.getProjectName());
				String context = msgRecDto.getContent();
				appPushService.pushMessage(String.valueOf(msgRecDto.getReceiverId()), context, msgRecDto.getTitle(),
						pushMap);

				// 消息ID,Set
				noticeIdSet.add(String.valueOf(msgRecDto.getNoticeId()));
				// 消息接收人ID'S
				if (StringUtils.isNotEmpty(msgRecIds)) {
					msgRecIds += ",";
				}
				msgRecIds += String.valueOf(msgRecDto.getId());
			}
			// 需更新消息ID'S
			if (!noticeIdSet.isEmpty()) {
				for (String noticeId : noticeIdSet) {
					if (StringUtils.isNotEmpty(noticeIds)) {
						noticeIds += ",";
					}
					noticeIds += noticeId;
				}
			}
			// 更新消息
			NoticeDto noticeDto = new NoticeDto();
			noticeDto.setIsSend(IsSendEnum.SEND.getNo());
			noticeService.updates(noticeDto, noticeIds);
			// 更新接收人
			MessageReceiverDto updateMRDto = new MessageReceiverDto();
			updateMRDto.setIsSend(IsSendEnum.SEND.getNo());
			messageReceiverService.updates(updateMRDto, msgRecIds);
		}
	}
}